/*     */ package hmc;
/*     */ 
/*     */ import ch.ethz.ssh2.Connection;
/*     */ import ch.ethz.ssh2.Session;
/*     */ import ch.ethz.ssh2.StreamGobbler;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import javax.swing.JDesktopPane;
/*     */ import javax.swing.JOptionPane;
/*     */ import javax.swing.JTextPane;
/*     */ import javax.swing.text.BadLocationException;
/*     */ import javax.swing.text.Document;
/*     */ import javax.swing.text.Position;
/*     */ import javax.swing.text.StyledDocument;
/*     */ import javax.swing.text.html.HTMLEditorKit;
/*     */ 
/*     */ public class SSHCommunicator
/*     */ {
/*     */   public Communicator comm;
/*     */   public String portName;
/*     */   private Session sess;
/*     */   private Connection conn;
/*     */   private InputStream in;
/*     */   private OutputStream out;
/*     */   private InputStream err;
/*  74 */   private boolean success = false;
/*     */   TopLevelGUI TLGUI;
/*     */   BufferedWriter fileOutput;
/*  80 */   String command = "";
/*     */   String compareItem;
/*  83 */   boolean toFile = false;
/*     */ 
/*  85 */   int lineNumber = 0;
/*     */   public int dataLogStartLine;
/*     */   FileWriter fstream;
/*     */   public File logFile;
/*     */   public String username;
/*     */   private String password;
/*     */   private String shelltype;
/*  93 */   private boolean CD = false;
/*  94 */   public boolean running = true;
/*  95 */   private boolean loginWorking = false;
/*     */ 
/*     */   public SSHCommunicator(String x, TopLevelGUI TLGUI)
/*     */   {
/*  99 */     this.portName = x;
/* 100 */     this.TLGUI = TLGUI;
/*     */   }
/*     */ 
/*     */   public void setSPC(Communicator x, boolean fromCD) {
/* 104 */     this.comm = x;
/* 105 */     this.comm.setVisible(fromCD);
/*     */   }
/*     */ 
/*     */   public void getLoginInfo()
/*     */   {
/*     */     try {
/* 111 */       this.CD = false;
/* 112 */       SSHLogin SSHL = new SSHLogin(this);
/* 113 */       SSHL.show();
/* 114 */       this.TLGUI.desktop.add(SSHL);
/* 115 */       SSHL.moveToFront();
/* 116 */       SSHL.setSelected(true);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 121 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void getLoginInfoFromCD() {
/* 126 */     this.CD = true;
/* 127 */     this.username = ((String[])TopLevelGUI.ConnectionDirectory.get(this.portName))[2];
/* 128 */     this.password = ((String[])TopLevelGUI.ConnectionDirectory.get(this.portName))[3];
/* 129 */     this.shelltype = ((String[])TopLevelGUI.ConnectionDirectory.get(this.portName))[4];
/*     */   }
/*     */ 
/*     */   public void login(String un, String pw, String st) {
/* 133 */     this.comm.setVisible(true);
/* 134 */     this.username = un;
/* 135 */     this.password = pw;
/* 136 */     this.shelltype = st;
/* 137 */     if (this.CD)
/* 138 */       this.conn = new Connection(((String[])TopLevelGUI.ConnectionDirectory.get(this.portName))[1]);
/*     */     else {
/* 140 */       this.conn = new Connection(this.portName);
/*     */     }
/* 142 */     this.comm.JavaPrinting("\nAttempting to Connect...\n");
/*     */ 
/* 144 */     Thread log = new Thread()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try {
/* 149 */           SSHCommunicator.access$002(SSHCommunicator.this, true);
/* 150 */           SSHCommunicator.this.conn.connect();
/* 151 */           SSHCommunicator.this.comm.JavaPrinting("\nConnected.\n\nAttempting to Authenticate...\n");
/* 152 */           SSHCommunicator.this.comm.JavaPrinting("\nAuthentication Successful?: " + SSHCommunicator.this.conn.authenticateWithPassword(SSHCommunicator.this.username, SSHCommunicator.this.password) + "\n ");
/* 153 */           SSHCommunicator.access$302(SSHCommunicator.this, SSHCommunicator.this.conn.openSession());
/*     */ 
/* 155 */           SSHCommunicator.this.sess.requestPTY(SSHCommunicator.this.shelltype);
/*     */ 
/* 157 */           SSHCommunicator.this.sess.startShell();
/*     */ 
/* 159 */           SSHCommunicator.this.startShell();
/* 160 */           SSHCommunicator.access$002(SSHCommunicator.this, false);
/*     */         }
/*     */         catch (IOException ioe)
/*     */         {
/* 164 */           ioe.printStackTrace();
/* 165 */           SSHCommunicator.this.comm.dispose();
/* 166 */           JOptionPane.showMessageDialog(null, "Unable to connect.", "ERROR", 0);
/*     */         }
/*     */         catch (IllegalStateException ise)
/*     */         {
/* 170 */           ise.printStackTrace();
/* 171 */           SSHCommunicator.this.comm.dispose();
/* 172 */           JOptionPane.showMessageDialog(null, "Could not authenticate.", "ERROR", 0);
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 176 */           e.printStackTrace();
/* 177 */           SSHCommunicator.this.comm.dispose();
/*     */         }
/*     */       }
/*     */     };
/* 182 */     log.start();
/*     */   }
/*     */ 
/*     */   public void reLogin()
/*     */   {
/* 199 */     login(this.username, this.password, this.shelltype);
/*     */   }
/*     */ 
/*     */   public boolean safeToDisconnect() {
/* 203 */     return !this.loginWorking;
/*     */   }
/*     */ 
/*     */   public void startShell() {
/* 207 */     Thread dt = new Thread()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try {
/* 212 */           SSHCommunicator.this.running = true;
/* 213 */           SSHCommunicator.access$502(SSHCommunicator.this, new StreamGobbler(SSHCommunicator.this.sess.getStdout()));
/* 214 */           SSHCommunicator.access$602(SSHCommunicator.this, SSHCommunicator.this.sess.getStdin());
/* 215 */           SSHCommunicator.access$702(SSHCommunicator.this, new StreamGobbler(SSHCommunicator.this.sess.getStderr()));
/*     */ 
/* 217 */           while (SSHCommunicator.this.running)
/*     */           {
/*     */             try
/*     */             {
/* 222 */               byte[] buffer = new byte[1024];
/* 223 */               boolean backspaced = false;
/* 224 */               int len = 0;
/*     */               int data;
/* 227 */               if ((data = SSHCommunicator.this.in.read()) > -1)
/*     */               {
/* 229 */                 buffer[(len++)] = (byte)data;
/*     */               }
/*     */ 
/* 232 */               String dat = new String(buffer, 0, len);
/*     */ 
/* 234 */               if (SSHCommunicator.this.err.available() > 0)
/*     */               {
/* 236 */                 if ((data = SSHCommunicator.this.err.read()) > -1)
/*     */                 {
/* 238 */                   buffer[(len++)] = (byte)data;
/*     */                 }
/*     */               }
/*     */ 
/* 242 */               String daterr = new String(buffer, 0, len);
/*     */ 
/* 244 */               if (dat.codePointAt(0) == 8)
/*     */               {
/* 246 */                 backspaced = true;
/* 247 */                 StyledDocument doc = SSHCommunicator.this.comm.ConsoleBox.getStyledDocument();
/* 248 */                 if (doc != null)
/*     */                   try {
/* 250 */                     doc.remove(doc.getLength() - 1, 1);
/* 251 */                     SSHCommunicator.this.comm.ConsoleBox.setStyledDocument(doc);
/* 252 */                     SSHCommunicator.this.comm.ConsoleBox.setCaretPosition(SSHCommunicator.this.comm.ConsoleBox.getDocument().getLength());
/*     */                   }
/*     */                   catch (Exception e)
/*     */                   {
/*     */                   }
/*     */               }
/* 258 */               if (!backspaced)
/*     */               {
/* 260 */                 int pos = SSHCommunicator.this.comm.ConsoleBox.getDocument().getLength();
/*     */ 
/* 262 */                 SSHCommunicator.this.comm.append(dat);
/*     */ 
/* 264 */                 if (pos != SSHCommunicator.this.comm.ConsoleBox.getDocument().getLength())
/* 265 */                   SSHCommunicator.this.comm.ConsoleBox.setCaretPosition(SSHCommunicator.this.comm.ConsoleBox.getDocument().getLength());
/*     */               }
/* 267 */               backspaced = false;
/*     */             }
/*     */             catch (Exception e)
/*     */             {
/*     */             }
/*     */ 
/*     */           }
/*     */ 
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 282 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     };
/* 287 */     dt.start();
/*     */   }
/*     */ 
/*     */   public void writeLogFile(File logFile, boolean HTMLOutput, int dataLogStartLine)
/*     */   {
/*     */     try {
/* 293 */       this.fstream = new FileWriter(logFile);
/* 294 */       this.fileOutput = new BufferedWriter(this.fstream);
/* 295 */       SimpleDateFormat custDateFormat = new SimpleDateFormat("EEE MMM-dd HH:mm:ss.SSS zzz yyyy");
/* 296 */       Date date = new Date();
/* 297 */       this.fileOutput.write("[" + custDateFormat.format(date) + "]\n");
/*     */ 
/* 299 */       if (!HTMLOutput)
/*     */       {
/* 301 */         String text = this.comm.ConsoleBox.getText();
/* 302 */         String[] temp = text.split("\n");
/*     */ 
/* 304 */         for (int x = dataLogStartLine; x < temp.length; x++)
/*     */         {
/* 306 */           this.fileOutput.write(temp[x]);
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 311 */         StyledDocument doc = this.comm.ConsoleBox.getStyledDocument();
/*     */ 
/* 313 */         HTMLEditorKit kit = new HTMLEditorKit();
/*     */         try
/*     */         {
/* 317 */           kit.write(this.fileOutput, doc, doc.getStartPosition().getOffset(), doc.getLength());
/*     */         }
/*     */         catch (FileNotFoundException e)
/*     */         {
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/*     */         }
/*     */         catch (BadLocationException e)
/*     */         {
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 333 */       this.fileOutput.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 337 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void sendCommand(String str)
/*     */   {
/*     */     try
/*     */     {
/* 345 */       this.command = (str + "\r");
/*     */ 
/* 347 */       byte[] c = this.command.getBytes();
/*     */ 
/* 349 */       for (int x = 0; x < c.length; x++)
/*     */       {
/*     */         try
/*     */         {
/* 353 */           this.out.write(c[x]);
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 365 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void simpleSendCommand(String str) {
/* 369 */     this.command = str.replace("\n", "\r");
/*     */     try
/*     */     {
/* 372 */       byte[] c = this.command.getBytes();
/*     */ 
/* 374 */       for (int x = 0; x < c.length; x++)
/*     */       {
/*     */         try
/*     */         {
/* 378 */           this.out.write(c[x]);
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 390 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void closeStreams()
/*     */   {
/*     */     try {
/* 396 */       if (this.conn.isAuthenticationComplete())
/* 397 */         this.comm.JavaPrinting("\nDisconnected.\n");
/* 398 */       this.running = false;
/* 399 */       this.sess.close();
/* 400 */       this.conn.close();
/*     */     }
/*     */     catch (NullPointerException npe)
/*     */     {
/*     */     }
/*     */   }
/*     */ }

/* Location:           F:\6-30-2012\Hazell\
 * Qualified Name:     hmc.SSHCommunicator
 * JD-Core Version:    0.6.0
 */